﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Jy123.Web.Module.Venues
{
    public class GetFlashXML : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strMId = context.Request.QueryString["ModuleId"];
            if (!string.IsNullOrEmpty(strMId))
            {
                int mId = int.Parse(strMId);
                BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                Model.Module_Main module = BLLModule.GetModel(mId);
                string strWhere = string.Format("FModule = {0} AND FAnnexe NOT IN (SELECT ID FROM Public_Annexe WHERE FAType = 8)", strMId);
                string strCarWhere = string.Format("FModule = {0} AND FAnnexe IN (SELECT ID FROM Public_Annexe WHERE FAType = 8)", strMId);
                Jy123.BLL.Public_Annexe BLLAnnexe = new Jy123.BLL.Public_Annexe();
                Jy123.BLL.Annexe.Annexe BLLAAnnexe = new Jy123.BLL.Annexe.Annexe();
                Jy123.BLL.Annexe_Record BLLRecord = new Jy123.BLL.Annexe_Record();
                List<Jy123.Model.Annexe_Record> listRecord = BLLRecord.GetModelList(strWhere);
                List<Jy123.Model.Annexe_Record> listCarRecord = BLLRecord.GetModelList(strCarWhere);
                List<Jy123.Model.Public_Annexe> listAllAnnexe = BLLAnnexe.GetModelList("FAType <> 6");
                List<Jy123.Model.Public_Annexe> listADAnnexe = BLLAnnexe.GetModelList("FAType = 6");
                Dictionary<int, Jy123.Model.Public_Annexe> dicAnnexe = new Dictionary<int, Jy123.Model.Public_Annexe>();
                foreach (Jy123.Model.Public_Annexe annexe in listAllAnnexe)
                {
                    dicAnnexe.Add(annexe.ID, annexe);
                }
                string xml = context.Cache["xmlvenues"].ToString();
                string strBackImg = string.Empty;
                string strBtnImg = string.Empty;
                if (module.FJType.Value == 4)
                {
                    strBackImg = "/Cache/Module/Flash/CourtImages2/Court_background.png";
                    strBtnImg = "/Cache/Module/Flash/CourtImages2/CourtBtn_background.jpg";
                }
                else if (module.FJType.Value == 6)
                {
                    strBackImg = "/Cache/Module/Flash/CourtImages/Court_background.png";
                    strBtnImg = "/Cache/Module/Flash/CourtImages/CourtBtn_background.jpg";
                }
                else if (module.FJType.Value == 28 || module.FJType.Value == 32)
                {
                    foreach (Jy123.Model.Annexe_Record record in listCarRecord)
                    {
                        if (record.IsUsed)
                        {
                            Jy123.Model.Public_Annexe cAnnexe = dicAnnexe[record.FAnnexe.Value];
                            strBackImg = cAnnexe.Model;
                            strBtnImg = "/Cache/Module/Flash/CourtImages/CourtBtn_backgroundcar.jpg";
                        }
                    }
                }
                else if (module.FJType.Value == 24)
                {
                    BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                    Model.TCenter_Main TCenter = BLLTCenter.GetModel(module.ID);
                    BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                    Model.Public_Level level = BLLLevel.GetModel(TCenter.FLevel.Value);
                    strBackImg = level.Model;
                    strBtnImg = "/Cache/Module/Flash/CourtImages/CourtBtn_background.jpg";
                }
                if (string.IsNullOrEmpty(strBackImg))
                {
                    strBackImg = "/Cache/Module/Flash/CourtImages2/Court_background.png";
                }
                if (string.IsNullOrEmpty(strBtnImg))
                {
                    strBtnImg = "/Cache/Module/Flash/CourtImages2/CourtBtn_background.jpg";
                }
                xml = xml.Replace("$BackImg$", strBackImg);
                xml = xml.Replace("$ButtonImg$", strBtnImg);
                StringBuilder partString = new StringBuilder();
                int ad2Mark = 0;
                int ad3Mark = 0;
                string strRelative = string.Empty;
                foreach (Jy123.Model.Annexe_Record record in listRecord)
                {
                    Jy123.Model.Public_Annexe cAnnexe = dicAnnexe[record.FAnnexe.Value];
                    int type = cAnnexe.FAType.Value;
                    if (type != Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.AD) && type != Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.Billboard))
                    {
                        string strPart = context.Cache["xmlvenuespart"].ToString();
                        string partId = cAnnexe.ID.ToString();
                        strPart = strPart.Replace("$PartId$", partId);
                        if (type == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.Pavilion))
                        {
                            strRelative = strRelative + partId + ",";
                        }
                        strPart = strPart.Replace("$DisplayOrder$", cAnnexe.DisplayOrder.Value.ToString());
                        string strIsShow = "1";
                        if (!record.IsUsed)
                        {
                            strIsShow = "0";
                        }
                        strPart = strPart.Replace("$IsShow$", strIsShow);
                        strPart = strPart.Replace("$PosX$", cAnnexe.PosX.Value.ToString());
                        strPart = strPart.Replace("$PosY$", cAnnexe.PosY.Value.ToString());
                        strPart = strPart.Replace("$DisplayName$", cAnnexe.AName);
                        strPart = strPart.Replace("$ImageUrl$", cAnnexe.Model);
                        strPart = strPart.Replace("$BtnImageUrl$", cAnnexe.SmallModel);
                        partString.Append(strPart);
                    }
                }
                xml = xml.Replace("$Part$", partString.ToString());
                if (strRelative.Length > 0)
                {
                    strRelative = strRelative.Substring(0, strRelative.Length - 1);
                }
                foreach (Jy123.Model.Annexe_Record record in listRecord)
                {
                    Jy123.Model.Public_Annexe cAnnexe = dicAnnexe[record.FAnnexe.Value];
                    int type = cAnnexe.FAType.Value;
                    string strIsShow = "1";
                    if (!record.IsUsed)
                    {
                        strIsShow = "0";
                    }
                    if (type == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AnnexeType.Billboard))//需要改进
                    {
                        switch (cAnnexe.ADLevel.Value)
                        {
                            case 1:
                                string strAd1 = context.Cache["xmlvenuesad1"].ToString();
                                List<Jy123.Model.Public_Annexe> ad1List = BLLAAnnexe.GetAListByCd(1, 3, 6);
                                string ad1String = ReplaceAdString(ad1List, listRecord, record, context);
                                strAd1 = strAd1.Replace("$ImageUrl$", cAnnexe.Model);
                                strAd1 = strAd1.Replace("$BtnImageUrl$", cAnnexe.SmallModel);
                                strAd1 = strAd1.Replace("$Relative$", strRelative);
                                strAd1 = strAd1.Replace("$PosX$", cAnnexe.PosX.Value.ToString());
                                strAd1 = strAd1.Replace("$IsShow$", strIsShow);
                                strAd1 = strAd1.Replace("$Option$", ad1String);
                                strAd1 = strAd1.Replace("$Id$", cAnnexe.ID.ToString());
                                xml = xml.Replace("$AD1$", strAd1);
                                break;
                            case 2:
                                string strAd2 = context.Cache["xmlvenuesad2"].ToString();
                                List<Jy123.Model.Public_Annexe> ad2List = BLLAAnnexe.GetAListByCd(2, 3, 6);
                                string ad2String = ReplaceAdString(ad2List, listRecord, record, context);
                                if (ad2Mark == 0)
                                {
                                    strAd2 = strAd2.Replace("$FirstOption$", ad2String);
                                    ad2Mark = 1;
                                }
                                else
                                {
                                    strAd2 = strAd2.Replace("$SecondOption$", ad2String);
                                    ad2Mark = 0;
                                }
                                strAd2 = strAd2.Replace("$ImageUrl$", cAnnexe.Model);
                                strAd2 = strAd2.Replace("$BtnImageUrl$", cAnnexe.SmallModel);
                                strAd2 = strAd2.Replace("$Relative$", strRelative);
                                strAd2 = strAd2.Replace("$PosX$", cAnnexe.PosX.Value.ToString());
                                strAd2 = strAd2.Replace("$IsShow$", strIsShow);
                                strAd2 = strAd2.Replace("$Id$", cAnnexe.ID.ToString());
                                xml = xml.Replace("$AD2$", strAd2);
                                break;
                            case 3:
                                string strAd3 = context.Cache["xmlvenuesad3"].ToString();
                                List<Jy123.Model.Public_Annexe> ad3List = BLLAAnnexe.GetAListByCd(3, 3, 6);
                                string ad3String = ReplaceAdString(ad3List, listRecord, record, context);
                                if (ad3Mark == 0)
                                {
                                    strAd3 = strAd3.Replace("$FirstOption$", ad3String);
                                    ad3Mark = 1;
                                }
                                else
                                {
                                    strAd3 = strAd3.Replace("$SecondOption$", ad3String);
                                    ad3Mark = 0;
                                }
                                strAd3 = strAd3.Replace("$ImageUrl$", cAnnexe.Model);
                                strAd3 = strAd3.Replace("$BtnImageUrl$", cAnnexe.SmallModel);
                                strAd3 = strAd3.Replace("$Relative$", strRelative);
                                strAd3 = strAd3.Replace("$PosX$", cAnnexe.PosX.Value.ToString());
                                strAd3 = strAd3.Replace("$IsShow$", strIsShow);
                                strAd3 = strAd3.Replace("$Id$", cAnnexe.ID.ToString());
                                xml = xml.Replace("$AD3$", strAd3);
                                break;
                            case 4://电子屏
                                string strAd4 = context.Cache["xmlvenuesscreen"].ToString();
                                List<Jy123.Model.Public_Annexe> ad4List = BLLAAnnexe.GetAListByCd(4, 3, 6);
                                string ad4String = ReplaceAdString(ad4List, listRecord, record, context);
                                strAd4 = strAd4.Replace("$Option$", ad4String);
                                strAd4 = strAd4.Replace("$ImageUrl$", cAnnexe.Model);
                                strAd4 = strAd4.Replace("$BtnImageUrl$", cAnnexe.SmallModel);
                                strAd4 = strAd4.Replace("$Relative$", strRelative);
                                strAd4 = strAd4.Replace("$PosX$", cAnnexe.PosX.Value.ToString());
                                strAd4 = strAd4.Replace("$IsShow$", strIsShow);
                                strAd4 = strAd4.Replace("$Id$", cAnnexe.ID.ToString());
                                xml = xml.Replace("$Screen$", strAd4);
                                break;
                        }
                    }
                }
                //清除没被替换的占位符
                xml = xml.Replace("$Part$", string.Empty);
                xml = xml.Replace("$AD1$", string.Empty);
                xml = xml.Replace("$AD2$", string.Empty);
                xml = xml.Replace("$AD3$", string.Empty);
                xml = xml.Replace("$Screen$", string.Empty);
                xml = xml.Replace("$Option$", string.Empty);

                context.Response.ContentType = "text/xml";
                byte[] byBuffer = System.Text.Encoding.Default.GetBytes(xml);
                context.Response.BinaryWrite(byBuffer);
            }
        }

        private string ReplaceAdString(List<Jy123.Model.Public_Annexe> listAnnexe, List<Jy123.Model.Annexe_Record> listRecord, Jy123.Model.Annexe_Record record, HttpContext context)
        {
            string opString = context.Cache["xmlvenuesop"].ToString();
            StringBuilder sbString = new StringBuilder();
            int mark = 0;
            for (int i = 1; i < listAnnexe.Count; i++)
            {
                string op2String = opString;
                Jy123.Model.Public_Annexe ad2Annexe = listAnnexe[i];
                op2String = op2String.Replace("$DisplayName$", ad2Annexe.AName);
                op2String = op2String.Replace("$ValueId$", ad2Annexe.ID.ToString());
                op2String = op2String.Replace("$ImageUrl$", ad2Annexe.Model);
                if (IsInList(ad2Annexe, listRecord, record))
                {
                    op2String = op2String.Replace("$Default$", "true");
                    mark = mark + 1;
                }
                else
                {
                    op2String = op2String.Replace("$Default$", "false");
                }
                sbString.Append(op2String);
            }
            //注意，广告内容只有三个选项，当用户没有选择广告内容或者广告内容被冲击掉，就默认第一个广告内容选            //中
            //if (listAnnexe.Count > 0)
            //{
            //    string opFirstString = opString;
            //    Jy123.Model.Public_Annexe adCAnnexe = listAnnexe[0];
            //    opFirstString = opFirstString.Replace("$DisplayName$", adCAnnexe.AName);
            //    opFirstString = opFirstString.Replace("$ValueId$", adCAnnexe.ID.ToString());
            //    opFirstString = opFirstString.Replace("$ImageUrl$", adCAnnexe.Model);
            //    if (mark == 0)
            //    {
            //        opFirstString = opFirstString.Replace("$Default$", "true");
            //    }
            //    else
            //    {
            //        opFirstString = opFirstString.Replace("$Default$", "false");
            //    }
            //    sbString.Insert(0, opFirstString);
            //}

            return sbString.ToString();
        }

        /// <summary>
        /// 判断当前广告内容是不是当前广告牌所选择的
        /// </summary>
        /// <param name="annexe">当前广告内容</param>
        /// <param name="listRecord">当前组件所有附属建筑记录</param>
        /// <param name="cRecord">当前广告牌记录</param>
        /// <returns></returns>
        private bool IsInList(Jy123.Model.Public_Annexe annexe, List<Jy123.Model.Annexe_Record> listRecord, Jy123.Model.Annexe_Record cRecord)
        {
            foreach (Jy123.Model.Annexe_Record record in listRecord)
            {
                if (record.AFather.HasValue && record.FAnnexe.HasValue)
                {
                    if (record.AFather.Value == cRecord.ID && record.FAnnexe.Value == annexe.ID)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
